<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/1/6
 * Time: 19:09
 */

namespace App\Http\Logics\Admin;


use App\Vip;
use App\VipCard;
use App\VipCardRule;
use App\VipCardRuleValue;
use Illuminate\Support\Facades\DB;
use App\Http\Logics\BaseLogic;
class VipCardLogic extends BaseLogic
{
    /**
     * 创建VIP卡
     * @param $data VIP卡信息
     * @return mixed 返回结果
     */
    public static function createVipCard($data)
    {
        try {
            // 创建事务，新添加会员卡的同时添加会员卡规格值和会员卡中间表的数据
            DB::transaction(function () use ($data) {
                $response = self::getPartnerIdByLoginSession($data['loginSession']);
                if ($response->code) {
                    return self::response(self::CONTENT_IS_EMPTY);
                }
                $partner_id = $response->data;
                $vip_card = new VipCard();
                $vip_card->partner_id = $partner_id;
                $vip_card->name = $data['name'];
                $vip_card->denomination = $data['denomination'];
                $vip_card->minimum_recharge = $data['minimum_recharge'];
                $vip_card->summary = $data['summary'];
                $url = $data['cover'] . "?imageInfo";
                // 处理封面图片
                $rs = dealHttp($url);
                $width = $rs->width;
                $height = $rs->height;
                $vip_card->cover_width = $width;
                $vip_card->cover_height = $height;
                $vip_card->cover = $data['cover'];
                if (!$vip_card->save()) {
                    return self::response(self::OBJECT_SAVE_FAIL);
                }


                // 注意：新增会员卡后要给进行会员卡规则的初始化，给该合作者的所有会员卡规则初始化会员卡的规则值
                $vip_card_id = $vip_card->id;
                $vip_card_rule = VipCardRule::where('partner_id', $partner_id)->get();
                foreach ($vip_card_rule as $item) {
                    $vip_card_rule_value = new VipCardRuleValue();
                    $vip_card_rule_value->vip_card_id = $vip_card_id;
                    $vip_card_rule_value->vip_card_rule_id = $item->id;
                    if (!$vip_card_rule_value->save()) {
                        return self::response(self::OBJECT_SAVE_FAIL);
                    }
                }
                // 注意：新增会员卡后要给进行会员卡规则的初始化，给该合作者的所有会员卡规则初始化会员卡的规则值
            });

            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 更新VIP卡
     * @param $data VIP卡信息
     * @return mixed 返回结果
     */
    public static function updateVipCard($data)
    {
        try {
            $vip_card = VipCard::find($data['id']);
            if (empty($vip_card)) {
                return self::response(self::CONTENT_IS_EMPTY);
            }
            $vip_card->name = $data['name'];
            $vip_card->denomination = $data['denomination'];
            $vip_card->minimum_recharge = $data['minimum_recharge'];
            $vip_card->summary = $data['summary'];
            $vip_card->cover = $data['cover'];
            if (!$vip_card->save()) {
                return self::response(self::OBJECT_SAVE_FAIL);
            }
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 删除VIP卡
     * @param $data 筛选条件
     * @return mixed 返回结果
     * @throws \Exception
     */
    public static function deleteVipCard($data)
    {
        try {
            // 创建事务，删除会员卡的同时，删除该会员卡与会员卡规则值得中间值
            DB::transaction(function () use ($data) {
                $vip_card = VipCard::find($data['vip_card_id']);

                $vip_card_rule_value = VipCardRuleValue::where('vip_card_id', $vip_card->id)->get();
                if (empty($vip_card_rule_value)) {
                    return self::response(self::CONTENT_IS_EMPTY);
                }

                foreach ($vip_card_rule_value as $item) {
                    if (!$item->delete()) {
                        return self::response(self::OBJECT_DELETE_FAILD);
                    }
                }

                if (!$vip_card->delete()) {
                    return self::response(self::OBJECT_DELETE_FAILD);
                }
            });
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 获取会员卡列表
     * @param $data 筛选条件
     * @return mixed 返回结果
     */
    public static function getVipCardList($data)
    {
        try {
            $response = self::getPartnerIdByKey($data['key']);
            if ($response->code) {
                return self::response($response->code);
            }
            $partner_id = $response->data;
            $vip_card = VipCard::where('partner_id', $partner_id)->orderBy('id', 'desc')->get();
            foreach ($vip_card as $item) {
                $vip_num = Vip::where('vip_card_id', $item->id)->where('partner_id', $partner_id)->count();
                $item->vip_num = $vip_num;
            }
            if (empty($vip_card)) {
                return self::response(self::CONTENT_IS_EMPTY);
            }
            return self::response(self::SUCCESS, $vip_card);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 获取会员卡详情
     * @param $data 筛选条件
     * @return mixed 返回结果
     */
    public static function getVipCardDetail($data)
    {
        try {
            $vip_card = VipCard::find($data['vip_card_id']);
            if (empty($vip_card)) {
                return self::response(self::CONTENT_IS_EMPTY);
            }
            return self::response(self::SUCCESS, $vip_card);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function getUserVipCard($data)
    {
        $response = self::getUserIdByLoginSession($data['loginSession']);
        if ($response->code){
            return self::response($response->code);
        }
        $user_id = $response->data;

        $vip = Vip::where('user_id', $user_id)->first();
        if (empty($vip)){
            return self::response(self::CONTENT_IS_EMPTY);
        }

        $vip_card = VipCard::with(['vipCardRule'])->find($vip->vip_card_id);
        if (empty($vip_card)){
            return self::response(self::CONTENT_IS_EMPTY);
        }

        foreach ($vip_card->vipCardRule as $item){
            $vip_card_rule_value = VipCardRuleValue::where('vip_card_rule_id', $item->id)->where('vip_card_id', $vip_card->id)->first();
            $item->discount = $vip_card_rule_value->discount;
        }
        if (empty($vip_card)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $vip_card);
    }
}